package com.kaesun.ufit.hardware;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.os.SystemClock;

public class Ds1307 {

	private static final int RTC_REG_SECOND = 0;
	private static final int RTC_REG_MINUTE = 1;
	private static final int RTC_REG_HOUR = 2;
	private static final int RTC_REG_DAY = 3;
	private static final int RTC_REG_DATE = 4;
	private static final int RTC_REG_MONTH = 5;
	private static final int RTC_REG_YEAR = 6;

	private RtcDate mRtcDate = new RtcDate(2014, 5, 1);
	private RtcTime mRtcTime = new RtcTime(9, 0, 0);


	public long getTimeInMillis() {
		return SystemClock.elapsedRealtime();
	}

	// (January 1 1970, 00:00:00.000 GMT)
	public long getTimeInMillis2() {
		Calendar cal = new GregorianCalendar();
		cal.set(mRtcDate.mYear, mRtcDate.mMonth, mRtcDate.mDate, mRtcTime.mHour, mRtcTime.mMinute, mRtcTime.mSecond);
		return cal.getTimeInMillis();
	}

	public void setRtcDate(int year, int month, int date) {
		int value;

		if (0 <= year) {
			mRtcDate.mYear = year;
			year -= 2000;
			value  = (year / 10) << 4;
			value += (year % 10);
			write(RTC_REG_YEAR, value);
		}

		if (0 <= month) {
			mRtcDate.mMonth = month;
			value  = (month / 10) << 4;
			value += (month % 10);
			write(RTC_REG_MONTH, value);
		}

		if (0 <= date) {
			mRtcDate.mDate = date;
			value  = (date / 10) << 4;
			value += (date % 10);
			write(RTC_REG_DATE, value);
		}
	}

	public RtcDate rtcDate() {
		return mRtcDate;
	}

	public void setRtcTime(int hour, int minute, int second) {
		int value;

		if (0 <= hour) {
			mRtcTime.mHour = hour;
			value  = (hour / 10) << 4;
			value += (hour % 10);
			write(RTC_REG_HOUR, value);
		}

		if (0 <= minute) {
			mRtcTime.mMinute = minute;
			value  = (minute / 10) << 4;
			value += (minute % 10);
			write(RTC_REG_MINUTE, value);
		}

		if (0 <= second) {
			mRtcTime.mSecond = second;
			value  = (second / 10) << 4;
			value += (second % 10);
			write(RTC_REG_SECOND, value);
		}
	}

	public RtcTime rtcTime() {
		return mRtcTime;
	}

	public RtcTime update() {
		int value = read(RTC_REG_SECOND);
		if (127 < value) {
			write(RTC_REG_SECOND, (value-128));	// rtc start
		}
		mRtcTime.mSecond  = (value >> 4) * 10;
		mRtcTime.mSecond += (value & 0xF);

		value = read(RTC_REG_MINUTE);
		mRtcTime.mMinute  = (value >> 4) * 10;
		mRtcTime.mMinute += (value & 0xF);

		value = read(RTC_REG_HOUR);
		mRtcTime.mHour  = (value >> 4) * 10;
		mRtcTime.mHour += (value & 0xF);

		mRtcDate.mDay = read(RTC_REG_DAY);

		value = read(RTC_REG_DATE);
		mRtcDate.mDate  = (value >> 4) * 10;
		mRtcDate.mDate += (value & 0xF);

		value = read(RTC_REG_MONTH);
		mRtcDate.mMonth  = (value >> 4) * 10;
		mRtcDate.mMonth += (value & 0xF);

		value = read(RTC_REG_YEAR);
		mRtcDate.mYear  = (value >> 4) * 10;
		mRtcDate.mYear += (value & 0xF);
		mRtcDate.mYear += 2000;

		return mRtcTime;
	}


	private int read(int reg) {
		int value = 0;

		try {
			Process su = Runtime.getRuntime().exec("i2cget -y -f 3 0x68 " + reg);
			BufferedReader br = new BufferedReader(new InputStreamReader(su.getInputStream()));
			String line = null;
			while ((line = br.readLine()) != null) {
				if (('0' == line.charAt(0)) && ('x' == line.charAt(1))) {
					value = Integer.parseInt(line.substring(2), 16);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return value;
	}

	private void write(int reg, int value) {
		try {
			Process su = Runtime.getRuntime().exec("i2cset -y -f 3 0x68 " + reg + " " + value);
			BufferedReader br = new BufferedReader(new InputStreamReader(su.getInputStream()));
			while (null != br.readLine()) {
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	public class RtcDate {
		public int mYear = 2014;
		public int mMonth = 5;
		public int mDate = 1;
		public int mDay = 5;	// 1(Sun), 2(Mon), ... , 7(Sat)

		public RtcDate(int year, int month, int date) {
			mYear = year;
			mMonth = month;
			mDate = date;
		}
	}

	public class RtcTime {
		public int mHour = 9;
		public int mMinute = 0;
		public int mSecond = 0;

		public RtcTime(int hour, int minute, int second) {
			mHour = hour;
			mMinute = minute;
			mSecond = second;
		}
	}
}
